from fastapi import APIRouter
import os
import enum
from weather_api import df

share_api = APIRouter()


class file_list(enum.Enum):
    file1 = 'AMap_adcode_citycode.xlsx'
    file2 = 'code_share.py'
    file3 = 'config.ini'
    file4 = 'config_read.py'
    file5 = 'main.py'
    file6 = 'place_api.py'
    file7 = 'pokemon_api.py'
    file8 = 'weather_api.py'


@share_api.get('/get')
async def code_share(file: file_list):
    my_file = file.value
    if my_file.endswith('.xlsx') :
        return df.to_json()
    else:
        code_list=[]
        encodings = ['utf-8', 'gbk']
        for encoding in encodings:
            try:
                with open(my_file, 'r', encoding=encoding) as f:
                    code_list = f.readlines()  # 按行读取，返回列表
                break  # 编码正确则跳出循环
            except UnicodeDecodeError:
                continue  # 编码错误则尝试下一种
    return {'mycode': code_list}
# print(os.listdir())
